package com.hmall.item.web;



import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hmall.common.dto.PageDTO;
import com.hmall.feignconfig.itemClient;
import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.inventory;
import com.hmall.item.service.IItemService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/item")
public class ItemController{

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IItemService itemService;


    @Autowired
    private com.hmall.feignconfig.itemClient itemClient;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> list(Integer page, Integer size){

        Page<Item> Page = PageHelper.startPage(page, size);
        List<Item> list = itemService.list();

        PageDTO<Item> dto = new PageDTO<Item>();
        dto.setList(list);
        dto.setTotal(Page.getTotal());

        return dto;
    }


    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable Long id){
        return itemService.getById(id);
    }

    /**
     * 添加
     * @param item
     */
    @PostMapping
    public void save(@RequestBody Item item){
       itemService.save(item);
    }


    /**
     * 修改状态
     * @param id
     * @param status
     * 商品状态 1-正常，2-下架，3-删除  317580
     */
    @PutMapping("/status/{id}/{status}")
    public void status(@PathVariable Long id,@PathVariable Integer status){

        Item byId = itemService.getById(id);
        byId.setStatus(status);
        itemService.updateById(byId);

        // 队列名称
        HashMap<String, Object> map = new HashMap<>();
        String queueName = "simple.queue";
        map.put("id",id);
        map.put("status",status);
        rabbitTemplate.convertAndSend(queueName,map);

    }

    /**
     * 修改
     * @param item
     */
    @PutMapping
    public void update(@RequestBody Item item){

        Long id = item.getId();

        Item byId = itemService.getById(id);
        Integer status = byId.getStatus();
        if (status == 2){
            itemService.updateById(item);
        }
    }

    /**
     * 删除
     * @param id
     */
    @DeleteMapping("/{id}")
    public void delect(@PathVariable Long id){

        Item byId = itemService.getById(id);
        Integer status = byId.getStatus();

        if (status == 2){
            itemService.removeById(id);
        }

    }

    @PutMapping("/stock/{itemId}/{num}")
    public void stock(@PathVariable("itemId") Long itemId,@PathVariable("num") Integer num){
        // 根据ID 查询
       // com.hmall.common.dto.Item byId = itemClient.getById(inventory.getItemId());

        Item byId = getById(itemId);
        byId.setStock(byId.getStock()-num);
        itemService.updateById(byId);
    }
}
